//引入axios库
import axios from "axios";
//引入querystring
import querystring from "querystring";
//设置基础地址
let baseUrl = '/api'

//请求拦截器
axios.interceptors.request.use((req) => {
  return req;
});

//响应拦截器
axios.interceptors.response.use((res) => {
    return res.data
});

/* 
封装 get
url， 
params ={}
*/

export const get = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .get(baseUrl+url, { params })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

/* 
封装 post
url， 
params ={}
isFile = false
*/

export const post = (url, params = {}, isFile = false) => {
  return new Promise((resolve, reject) => {
    let data = {};
    if (isFile) {
      data = new FormData();
      for (let i in params) {
        data.append(i, params[i]);
      }
    } else {
      data = querystring.stringify(params);
    }
    axios
      .post(baseUrl+url, data)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
